/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.comsrv.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.admin.api.feign.RemoteFileService;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.comsrv.entity.CnBanner;
import com.ykht.proj.comsrv.mapper.CnBannerMapper;
import com.ykht.proj.comsrv.service.CnBannerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 轮播图
 *
 * @author proj code generator
 * @date 2020-11-16 17:21:47
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class CnBannerServiceImpl extends ServiceImpl<CnBannerMapper, CnBanner> implements CnBannerService {

	private final RemoteFileService fileService;



	@Value("${oss.baseHost}")
	private String ossBaseHost;
	@Override
	public Map uploadFile(MultipartFile file) {
		R upload = fileService.upload(file);
		Map<String, String> resultMap = (Map<String, String>) upload.getData();
		String fileName = ossBaseHost+ resultMap.get("fileName");
		log.info("上传文件地址为：{}",fileName);
		resultMap.put("fileName",fileName);
		return resultMap;
	}

	@Override
	public R updateBanner(CnBanner cnBanner) {
		try {
			boolean b = querySort(cnBanner);
			if(b){
				this.updateById(cnBanner);
				return R.ok();
			}else {
				return R.failed("数字重复,请重新填写");
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.failed();
		}
	}


	@Override
	public R insertBanner(CnBanner cnBanner) {
		try {
			boolean b = querySort(cnBanner);
			if(b){
				this.save(cnBanner);
				return R.ok();
			}else {
				return R.failed("数字重复,请重新填写");
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.failed();
		}
	}

	private boolean querySort(CnBanner cnBanner){
		List<CnBanner> list = this.list();
		if(list!=null && list.size()>0){
			for (CnBanner o :list){
				if(o.getSort()!=null && cnBanner.getSort()!=null){
					if(cnBanner.getId()!=null){
						if(!cnBanner.getId().equals(o.getId())){
							if(o.getSort().equals(cnBanner.getSort())){
								return false;
							}
						}
					}else{
						if(o.getSort().equals(cnBanner.getSort())){
							return false;
						}
					}
				}
			}
		}
		return true;
	}
}
